package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buba.mapper.ConditionDetailsMapper;
import com.buba.mapper.FileMapper;
import com.buba.service.ConditionDetailsService;
import com.buba.service.FileService;
import entity.maintenance.MaintenanceWorkOrder;
import entity.maintenance.RepairOrder;
import entity.sparemanage.ConditionDetails;
import entity.sparemanage.File;
import entity.sparemanage.SpareLedger;
import entity.sparemanage.SpareUse;
import com.buba.mapper.SpareUseMapper;
import com.buba.service.SpareUseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import entity.sparemanage.dto.fileList;
import entity.sparemanage.dto.insertSpareUseDto;
import entity.sparemanage.dto.selAllSpareUseDto;
import entity.sparemanage.dto.selAllSpareUseWrapper;
import entity.sparemanage.vo.spareListUseVo;
import entity.system.CodeRules;
import feign.maintenanceFeign;
import feign.systemFeign;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.PageResult;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 备件领用 服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-05-18
 */
@Service
public class SpareUseServiceImpl extends ServiceImpl<SpareUseMapper, SpareUse> implements SpareUseService {

    @Autowired
    private SpareUseMapper spareUseMapper;

    @Autowired
    private systemFeign systemFeign;

    @Autowired
    private maintenanceFeign maintenanceFeign;


    @Autowired
    private FileService fileService;

    @Autowired
    private ConditionDetailsService conditionDetailsService;

    /* *
     * @Author: beisheng
     * @Descriprion: 全查模糊查分页查询备件领用单列表
     * @Date: 2023/6/2 9:01
     * @Return: common.R
     * @Param: [selAllSpareUseDto]
     */

    @Override
    public PageResult selAllSpareUse(selAllSpareUseDto selAllSpareUseDto) {

        Page<SpareUse> page = new Page<>(selAllSpareUseDto.getPage(),selAllSpareUseDto.getPageSize());
        Page<SpareUse> spareUsePage = spareUseMapper.selectPage(page, new selAllSpareUseWrapper().queryWrapper(selAllSpareUseDto));
        List<SpareUse> spareUseList = spareUseMapper.selectList(new QueryWrapper<SpareUse>().eq("delete_flag", 0));
        List<SpareUse> records = spareUsePage.getRecords();
        for (SpareUse record : records) {
            record.setUseApplicantName(systemFeign.selOnePeople(record.getUseApplicantid()).getUserName());
        }
        return new PageResult(selAllSpareUseDto.getPage(),selAllSpareUseDto.getPageSize(),spareUseList.size(),spareUsePage.getRecords());
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 新增领用申请
     * @Date: 2023/6/15 8:54
     * @Return: common.R
     * @Param: [spareUseDto]
     */

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSpareUse(insertSpareUseDto spareUseDto) {
        //1.查询编码生成规则，进行生成编码
        CodeRules bj = systemFeign.selAllRules("BJLY");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(bj.getDateTime());
        String format = simpleDateFormat.format(new Date());
        List<SpareUse> spareUseList = spareUseMapper.selectList(new QueryWrapper<SpareUse>().eq("delete_flag", 0));
        String code = bj.getPrefix()+format+"0"+(spareUseList.size()+1);
        //2.新增基本数据
        SpareUse spareUse = new SpareUse();
        spareUse.setSpUsecode(code);
        spareUse.setUseAudit(1);
        if(spareUseDto.getWorkType() == 1){
            MaintenanceWorkOrder oneMainWorkOrder = maintenanceFeign.getOneMainWorkOrder(spareUseDto.getWorkId());
            spareUse.setWorkCode(oneMainWorkOrder.getOrderNumber());
        }else {
            RepairOrder oneRepairOrders = maintenanceFeign.getOneRepairOrders(spareUseDto.getWorkId());
            spareUse.setWorkCode(oneRepairOrders.getOrderNumber());
        }
        spareUse.setCreateTime(new Date());
        spareUse.setUpdateTime(new Date());
        spareUse.setDeleteFlag(0);
        BeanUtils.copyProperties(spareUseDto,spareUse);
        try {
            spareUseMapper.insert(spareUse);
        }catch (Exception e){
            throw new RuntimeException("新增失败",e);
        }
        //3.新增关联备件信息
        List<spareListUseVo> spareList = spareUseDto.getSpareList();
        List<ConditionDetails> conditionDetailsList = new ArrayList<>();
        for (spareListUseVo spareListUseVo : spareList) {
            ConditionDetails conditionDetails = new ConditionDetails();
            conditionDetails.setUseId(spareUse.getSpId());
            conditionDetails.setSpId(spareListUseVo.getSpId());
            conditionDetails.setUseNumbers(spareListUseVo.getUseNumber());
            conditionDetails.setCreateTime(new Date());
            conditionDetails.setUpdateTime(new Date());
            conditionDetailsList.add(conditionDetails);
        }
        try {
            conditionDetailsService.saveBatch(conditionDetailsList);
        }catch (Exception e){
            throw new RuntimeException("失败",e);
        }
        //4.新增关联文档
        List<fileList> fileLists = spareUseDto.getFileList();
        List<File> fileArrayList = new ArrayList<>();
        for (fileList fileList : fileLists) {
            File file = new File();
            file.setFileName(fileList.getName());
            file.setFileType(2);
            file.setFileAddress(fileList.getUrl());
            file.setAssociationId(spareUse.getSpId());
            file.setCreateTime(new Date());
            file.setUpdateTime(new Date());
            fileArrayList.add(file);
        }
        try {
            fileService.saveBatch(fileArrayList);
        }catch (Exception e){
            throw new RuntimeException("新增关联图片失败",e);
        }
    }
}
